/* 
ajax封装
*/
import axios from "axios";
import jquery from "jquery";
import $loading from '@/components/Global'

import Vue from 'vue'
Vue.use($loading)
import {
  Message
} from "element-ui";
import {
  ciphertext
} from '@/api/api'
import store from '@/store'
import {
  Loading
} from 'element-ui';
// 引入local
import local from "@/utils/local";

// 大气，标准站
var baseURL
if (process.env.NODE_ENV === 'production') {
  if (host == "10.31.215.133:44308") { //内网
    baseURL = 'http://10.31.215.42/'
  } else {
    baseURL = 'http://10.46.81.26:10081'
  }
} else { // 开发环境
  baseURL = 'http://124.71.112.197:8002'
}

export const instance = axios.create({
  baseURL
});

// 水
var baseURL
if (process.env.NODE_ENV === 'production') {
  if (host == "10.31.215.133:44308") { //内网
    baseURL = 'http://10.31.215.64:9998/api'
  } else {
    baseURL = 'http://10.46.81.26:19998/api' //外网
  }
} else { // 开发环境
  baseURL = 'http://139.155.25.17:9998/api'
}

export const instance2 = axios.create({
  baseURL
});

// 井盖
var baseURL
if (process.env.NODE_ENV === 'production') {
  if (host == "10.31.215.133:44308") { //内网
    baseURL = 'http://10.31.215.42:8111/api/grpt/dev/well/base'
  } else {
    baseURL = 'http://10.46.81.26:8111/api/grpt/dev/well/base'
  }
} else { // 开发环境
  baseURL = 'http://114.117.196.141:10098/grpt/dev/well/base'
}

export const instance3 = axios.create({
  baseURL
});
var api = "";
var mw = ''
var sj = new Date().getTime()
var ssokey = ''
//  获取密文 ssokey

var host = window.location.host;
if (process.env.NODE_ENV === "production") {
  if (host == "10.31.215.133:44308") {
    //内网
    api = "http://10.31.215.133:44308";
  } else {
    api = "http://10.46.81.26:44308"; //外网
  }
} else {
  // 开发环境
  api = "http://36.111.191.34:44309";
}
var sj = new Date().getTime()

var mw = ''
// 获取ssokey
async function getssokey() {
  try {
    const response = await fetch(api + "/Home/GenerateCipher?timestamp=" + sj);
    const json = await response.text();
    const response2 = await fetch(api +  "/Home/GetSsoKey?sys=SSO&OriginalData=" + sj + "&Encrypteddata=" +  json);
    const ssokeyval = await response2.text();
    return ssokeyval
} catch(err) {
    console.error(err);
}
  // jquery.ajax({
  //   url: api + "/Home/GenerateCipher?timestamp=" + sj,
  //   type: "get",
  //   dataType: "text",
  //   success: function (res) {
  //     mw = "";
  //     mw = res.data;
  //     jquery.ajax({
  //       url:
  //         api +
  //         "/Home/GetSsoKey?sys=SSO&OriginalData=" +
  //         sj +
  //         "&Encrypteddata=" +
  //         res,
  //       type: "get",
  //       dataType: "text",
  //       success: function (res2) {
  //         ssokey = "";
  //         ssokey = res2;
  //         sessionStorage.removeItem('ssokey')
  //         sessionStorage.setItem('ssokey', res2)
  //       },
  //     });
  //   },
  // });
}

var host = window.location.host
if (process.env.NODE_ENV === 'production') {
  if (host == "10.31.215.133:44308") { //内网
    axios.defaults.baseURL = 'http://10.31.215.133:44308'
  } else {
    axios.defaults.baseURL = 'http://10.46.81.26:44308' //外网
  }
} else { // 开发环境
  axios.defaults.baseURL = 'http://36.111.191.34:44309'
}


//请求拦截

   axios.interceptors.request.use(async function(config) {
  
  Vue.$loading.show('loading...')
  var ssokey = ''
  ssokey = await getssokey() //调用ssokey

    config.headers = {
      
      ...config.headers,
      'Content-type': 'application/json;charset=utf-8',
      'sys': 'SSO',
      'ssokey': ssokey, //赋值
    }
  
  
  return config;
}),

  err => {
    return Promise.reject(err);
  }








// 响应拦截器
axios.interceptors.response.use(response => {
  if (!response) {
    Vue.$loading.hide()
  }
  if (response.status === 200) {
    //loading隐藏
    Vue.$loading.hide()
    let res = response;
    //统一处理
    if (res.code === 0) {

      Message({
        type: "success",
        message: res.msg
      });
    }
    if (res.code === 1 || res.code === 5001) {
      Message.error(res.msg);
    }

    return response;
  }


}),
  err => {
    return Promise.reject(err);
  };


export default axios;


